package com.hit.server.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.server.api.entity.CheckTaskTicketsEntity;
import com.hit.server.api.dto.CheckTaskTicketsQueryDTO;
import com.hit.server.api.type.DiscernTypeEnums;
import com.hit.server.api.type.TicketAllotStatusEnums;
import com.hit.server.api.type.TicketStatusEnums;
import com.hit.server.api.vo.CheckTaskTicketsVO;
import com.hit.server.biz.mapper.CheckTaskTicketsMapper;
import com.hit.server.api.service.CheckTaskTicketsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

/**
 * 检测任务工单
 *
 * @author kehanjiang
 * @date 2025-04-18 15:49:27
 */
@Service
@RequiredArgsConstructor
public class CheckTaskTicketsServiceImpl extends ServiceImpl<CheckTaskTicketsMapper, CheckTaskTicketsEntity> implements CheckTaskTicketsService {

    private CheckTaskTicketsVO convertToVO(Object o) {
        if (o instanceof CheckTaskTicketsEntity) {
            CheckTaskTicketsVO vo = new CheckTaskTicketsVO();
            BeanUtil.copyProperties(o, vo);
            //设置其他参数
            Optional.ofNullable(vo.getStatus()).ifPresent(status -> {
                vo.setStatusCn(TicketStatusEnums.getByCode(status).getDesc());
            });
            Optional.ofNullable(vo.getAllotStatus()).ifPresent(allotStatus -> {
                vo.setAllotStatusCn(TicketAllotStatusEnums.getByCode(allotStatus).getDesc());
            });
            return vo;
        }
        return null;
    }

    private void preCheck(CheckTaskTicketsEntity entity, Boolean update) {
        Long id = entity.getId();
        String name = entity.getName();
        if (update) {//更新操作
            Assert.notNull(id, "id不可为空！");
        } else {
            Assert.hasText(name, "name不可为空！");
        }
    }

    @Override
    public Page<CheckTaskTicketsVO> getCheckTaskTicketsPage(Page page, CheckTaskTicketsQueryDTO dto) {
        CheckTaskTicketsEntity entity = new CheckTaskTicketsEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setName(null);
        final String name = dto.getName();
        LambdaQueryWrapper<CheckTaskTicketsEntity> wrapper = new LambdaQueryWrapper<>(entity);
        wrapper.like(StringUtils.hasText(name), CheckTaskTicketsEntity::getName, name);
        wrapper.orderByAsc(CheckTaskTicketsEntity::getId);
        return (Page<CheckTaskTicketsVO>) this.page(page, wrapper)
                .convert(this::convertToVO);
    }

    @Override
    public CheckTaskTicketsVO getCheckTaskTicketsById(Long id) {
        Assert.notNull(id, "id不可为空！");
        return convertToVO(this.getById(id));
    }


    @Override
    public Boolean removeCheckTaskTicketsBatchByIds(List<Long> ids) {
        Assert.notEmpty(ids, "ID不可为空！");
        return this.removeBatchByIds(ids);
    }
}